package team.yidong.fenda.controller;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import team.yidong.fenda.entity.Answer;
import team.yidong.fenda.entity.Master;
import team.yidong.fenda.entity.User;
import team.yidong.fenda.service.*;

import javax.servlet.http.HttpSession;

/**
 * Created by xuanlong on 2017/11/10.
 */
@Controller
public class BlackListController {
    @Autowired
    BlackListService blackListService;
    @Autowired
    HospitalService hospitalService;
    @Autowired
    UserService userService;
    @Autowired
    MasterService masterService;
    @Autowired
    AnswerService answerService;

    @RequiresPermissions("blackList")
    @RequestMapping("/blackList")
    public String blackList(HttpSession session, Model model) {

        Integer hospitalId = (Integer) session.getAttribute("loginHospitalId");
        String sysName = (String) session.getAttribute("loginSysUserName");
        model.addAttribute("sysName", sysName);

        if (hospitalId != 1) {
            JsonConfig jsonConfig = new JsonConfig();
            jsonConfig.setExcludes(new String[]{"hospitalPermissionSet", "hospitalImgSet", "hospitalFieldList", "userSet", "masterList"});
            model.addAttribute("hospitals", JSONArray.fromObject(hospitalService.findHospitalById(hospitalId), jsonConfig));
        } else {
            JSONArray jsonArray = JSONArray.fromObject(hospitalService.findAll());
            jsonArray.remove(0);
            model.addAttribute("hospitals", jsonArray);
        }

        return "blackList";
    }

    @RequestMapping("/findBlackList")
    @ResponseBody
    public JSONObject findBlack(HttpSession session, String hospitalName, String name, String blackType) {

        Integer hospitalId = (Integer) session.getAttribute("loginHospitalId");
        return blackListService.findBlack(hospitalId, hospitalName, name, blackType);
    }

    @RequestMapping("/removeBlack")
    @ResponseBody

    public String removeBlack(Integer id, String kind) {
        blackListService.removeBlack(id, kind);
        return "移除成功";
    }

    @RequestMapping("/addUserBlack")
    @ResponseBody
    public void addMasterBlack(@RequestParam Integer userId, @RequestParam Integer questionId) {
        userService.changeUserBlack(userId, questionId);
    }

    @RequestMapping("/addMasterBlack")
    @ResponseBody

    public void addBlack(@RequestParam Integer masterId, @RequestParam Integer answerId) {
        Answer answer = answerService.findById(answerId);
        answer.setIsBlack("是");
        answerService.updateAnswer(answer);
        Master master = masterService.findById(masterId);
        master.setIsBlack("是");
        masterService.updateMaster(master);
    }
}
